﻿////////////////////////////////////////////////////////CA及加密狗相关操作/////////////////////////////////////////////////////

//加载证书
function LoadCACert(arrayCerts) {
    //主管端----------------------------------------------------------------------------
    var objCertList = $("select"); //定义<select>对象
    var ret = 0;
    //先清空CertList
    objCertList.empty();
    //把满足要求的证书加入CertList
    if (arrayCerts.length == 0) {
        var el = document.createElement("option");
        el.text = "没有找到数字证书";
        el.value = -1;
        objCertList.add(el);
    } else {
        //alert("bbb");
        for (var i = 0; i < arrayCerts.length; i++) {
            if (arrayCerts[i].Issuer.indexOf("CN=Flyedt Class 2 CA") >= 0 || (arrayCerts[i].KeyUsage & 0x80)) {
                var el = document.createElement("option");
                el.text = arrayCerts[i].CommonName;
                el.value = i;
                ret++;
                objCertList.append("<option value=" + i + ">" + arrayCerts[i].CommonName + "</option>");
            }
        }
    }

    return ret;
}

var keynum = 0;
var Message = "";

////取得Dogid
function getDog() {
    var obj2 = document.createElement("OBJECT");
    obj2.style.display = "none";

    obj2.classid = "CLSID:6844EFB1-7752-4424-9145-A12CC927B986";
    obj2.codeBase = "u.ocx";
    var r = "";
    try {
        document.body.appendChild(obj2);
        r = obj2.GetU(0) + "";
    }
    catch (err) {
        return "-1";
    }

    var temp = r;
    var dogtype = "";
    try {
        if (r == "0,0" || r == "-1") {
            temp = "-1";
        }
        else {
            if (r.indexOf(",") > -1) {
                temp = r.split(",")[1];
                dogtype = r.split(",")[0];
            }
            else {
                temp = r;
            }
        }

    }
    catch (err) {
        temp = "-1";
    }
    if (temp == null || temp == "" || temp == "-1" || temp == "0") {
        return temp == '0' ? '-1' : "-1";
    }
    else {
        return temp;
    }
}

////取得Key
function getKey() {
    try {
        keyinfo.pkcs11_dll = "ShuttleCsp11_3003.dll";
        keyinfo.sn_len = 30;

        keynum = keyinfo.refresh();
        if (keynum <= 0) {
            return '-1';
        }
        if (keynum > 1) {
            return "0";
        }
        sn = keyinfo.GetKeySN(0);
        return sn;
    }
    catch (err) {
        return '-1';
    }
}
///CA
function GetCACert() {
    var arrayIssuerDN = new Array(
            "C=CN, O=\"iTruschina Co., Ltd.\", OU=China Trust Network, OU=Terms of use at https://www.itrus.com.cn/ctnrpa (c)2007, CN=iTruschina CN Enterprise Individual Subscriber CA"
            , "CN=Flyedt Class 2 CA"
    //, "O=天威诚信（测试）, OU=天威诚信（测试）部, CN=天威诚信（测试）用户CA"
			, "C=CN, S=Guangdong, L=Shenzhen, O=ShenZhen Certificate Authority, OU=szca, CN=SZCA");

    var arrayCerts = filterCerts(arrayIssuerDN, 0, "");
    return LoadCACert(arrayCerts);
}

function GetCACertFirst() {
    var ret = "";
    var dogid = getDog();
    var keyid = getKey();
    if (keyid == "0") {
        return ret;
    }
    if (dogid != "-1" && keyid != "-1" && keyid != '0') {
        return ret;
    }
    if (dogid == "-1" && (keyid == "-1" || keyid == "0")) {
        if (keyid != "0") {
        }
        return ret;
    }
    var arrayIssuerDN = new Array(
            "C=CN, O=\"iTruschina Co., Ltd.\", OU=China Trust Network, OU=Terms of use at https://www.itrus.com.cn/ctnrpa (c)2007, CN=iTruschina CN Enterprise Individual Subscriber CA"
            , "CN=Flyedt Class 2 CA"
    //, "O=天威诚信（测试）, OU=天威诚信（测试）部, CN=天威诚信（测试）用户CA"
			, "C=CN, S=Guangdong, L=Shenzhen, O=ShenZhen Certificate Authority, OU=szca, CN=SZCA");

    var arrayCerts = filterCerts(arrayIssuerDN, 0, "");
    if (arrayCerts.length >= 1) {
        for (var i = 0; i < arrayCerts.length; i++) {
            if (arrayCerts[i].Issuer.indexOf("CN=Flyedt Class 2 CA") >= 0 || (arrayCerts[i].KeyUsage & 0x80)) {
                ret = arrayCerts[i];
                break;
            }
        }
    }
    return ret;
}

///条件验证
///返回值：数字,消息
///0：操作成功，key号已经写入cookie；
///1：有多个证书，显示证书选择层
///2：出现异常，返回异常消息
function CheckDogCA() {
//    if (!window.ActiveXObject) {
//        //alert("请使用 IE 内核浏览器进行投标报名。");
//        return "2," + escape("请使用 IE 内核浏览器进行登录。");
//    }
    dogid = getDog();
    keyid = getKey();

   
    if (keyid == "0") {
        Message = "检测到 " + keynum.toString() + " 个证书Key，请拔出不使用的Key。";
        return "2," + escape(Message);
    }
    if (dogid != "-1" && keyid != "-1" && keyid != '0') {

        Message = "检测到同时插入了身份认证卡和数字证书Key，请拔出不使用的设备";
        return "2," + escape(Message); ;
    }
    if (dogid == "-1" && (keyid == "-1" || keyid == "0")) {
        if (keyid != "0") {
            Message = "请插入数字证书设备。";
        }
        return "0,";
    }
    var certcnt = GetCACert();
  
    if (certcnt == 1) {
        //SetCookie("dogid", keyid);
        // postDog(keyid);
        return "0," + keyid.toString();
    }
//    else if (certcnt == 0 && keyid != '-1' && keyid != 0) {

//        Message = "您插入的Key中没有有效的数字证书。";
//        return "2," + escape(Message); ;
//    }
//    else if (certcnt > 0) {
//        alert(数字证书异常);
//        return "0," + keyid.toString();
//    }
    if (keyid > 0) {
        // SetCookie("dogid", keyid);
        return "0," + keyid.toString();
    }
    if (dogid != "-1") {
        // postDog(dogid);
        // SetCookie("dogid", dogid);
        return "0," + dogid.toString();
    }
}
